import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart'; // 导入包
import '../view/login_top_view.dart';
import '../view/regis_center_view.dart';
import '../../main.dart';
import '../../manger/qysdk_manger.dart';
import '../../manger/umevent_utils.dart';

class RegisView extends StatefulWidget {
  const RegisView({super.key});

  @override
  State<RegisView> createState() => _RegisViewState();
}

class _RegisViewState extends State<RegisView> {
  // 用来控制 RegisCenterView 的偏移量
  double _keyboardHeight = 0;

  @override
  Widget build(BuildContext context) {
    return KeyboardVisibilityProvider(
      child: Scaffold(
        body: Container(
          color: Color(0xffF5F5F5),
          child: Stack(
            children: [
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: SizedBox(
                  height: 260,
                  child: LoginTopView(
                    onBack: () {
                      Navigator.pop(context);
                    },
                    onHelp: () {
                      QYSDK.showQYSession();
                    },
                    type: 0,
                  ),
                ),
              ),
              Positioned(
                top: 215 - _keyboardHeight,  // 键盘高度的偏移量
                left: 25,
                right: 25,
                height: 570,
                child: RegisCenterView(
                  onRegisterSuccess: () {
                    pushMainPage();
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void pushMainPage() {
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (context) => MainPage(),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    UmengEvent.trackEvent("register_launch","进入注册页","");

    // 监听键盘显示和隐藏
    KeyboardVisibilityController().onChange.listen((isKeyboardVisible) {
      setState(() {
        // 如果键盘显示，调整 RegisCenterView 的位置
        _keyboardHeight = isKeyboardVisible ? 100 : 0;  // 偏移量根据键盘的高度设置
      });
    });
  }
}
